qx.Class.define("caraffa.forms.Caja",
{
	extend : qx.ui.form.ow.FormTab,

  construct : function (cajaTipo, cod_clinica)
  {
  	this.cajaTipo = cajaTipo;
  	this.cod_clinica = cod_clinica;
  	if (this.cod_clinica == 1){
  		var clinica = "ZIPOLI";
  	}
  	if (this.cod_clinica == 2){
  		var clinica = "AVELLANEDA";
  	}
  	this.base(arguments, "CajaActual" + clinica, "Caja Mensual: "+ clinica);    
    // RPC
    this.rpcCaja = new qx.io.remote.Rpc();
		this.rpcCaja.setTimeout(10000);
		this.rpcCaja.setUrl("services/");
		this.rpcCaja.setServiceName("Caja");

		try{
			this.resRubros = this.rpcCaja.callSync("getRubros");
		}
		catch(ex){
			alert("Error: " + ex);
		}	

		this.grp1 = new qx.ui.groupbox.GroupBox();
	  		this.grp1.setWidth(370);
	  		this.grp1.setHeight(402);
	  		this.grp1.setLayout(new qx.ui.layout.VBox(5));
	  		
	  		this.grp1Monto = new qx.ui.groupbox.GroupBox();
	  			this.grp1Monto.setLayout(new qx.ui.layout.Basic())			;
	  			 			  				
	  			this.lblIngresosT = new qx.ui.basic.Label("<b><big> Ingresos:</big></b>");
		  			this.lblIngresosT.setRich(true);
	  			this.lblIngresosV = new qx.ui.basic.Label("");
	  				this.lblIngresosV.setRich(true);
	  				
	  			this.lblEgresosT = new qx.ui.basic.Label("<b><big> Egresos:</big></b>");
		  			this.lblEgresosT.setRich(true);
	  			this.lblEgresosV = new qx.ui.basic.Label("");
	  				this.lblEgresosV.setRich(true);
	  			
	  			this.lblLineaResultado = new qx.ui.basic.Label("____________");
	  			
	  			this.lblMontoDisponibleT = new qx.ui.basic.Label("<b><big><u>Monto Disponible:</u></big></b>");
		  			this.lblMontoDisponibleT.setRich(true);
	  			this.lblMontoDisponibleV = new qx.ui.basic.Label("");
	  				this.lblMontoDisponibleV.setRich(true);
			
	  		this.grp1Monto.add(this.lblIngresosT,{left:10, top:10});
	  		this.grp1Monto.add(this.lblIngresosV,{left:200, top:10});
	  		this.grp1Monto.add(this.lblEgresosT,{left:10, top:30});
	  		this.grp1Monto.add(this.lblEgresosV,{left:200, top:30});
	  		this.grp1Monto.add(this.lblLineaResultado,{left:195, top:34});
	  		this.grp1Monto.add(this.lblMontoDisponibleT,{left:10, top:50});
	  		this.grp1Monto.add(this.lblMontoDisponibleV,{left:200, top:50});
	
	  		
	  		this.grp1Alta = new qx.ui.groupbox.GroupBox();
	  			this.grp1Alta.setLayout(new qx.ui.layout.VBox(5));
	  		
	  			this.rdbTipo = new qx.ui.form.ow.Radios("Tipo:",[{label:'Egreso', value:'E'},{label:'Ingreso', value:'I'}], true);
	  			this.cmbRubro = new qx.ui.form.ow.ComboFilter("Rubro:","",true);
	  			this.txaGlosa = new qx.ui.form.ow.TextArea("Descripción:","","",false);
	  			this.txtImporte = new qx.ui.form.ow.TextField("Importe:");
	  			this.grp1AltaBotonera = new qx.ui.groupbox.GroupBox();
	  				this.grp1AltaBotonera.setLayout(new qx.ui.layout.HBox(20).set({alignX:"center"}));
	  				this.btnAceptar = new qx.ui.form.Button("Aceptar");
	  				this.btnCancelar = new qx.ui.form.Button("Cancelar");
	  			this.grp1AltaBotonera.add(this.btnAceptar);
	  			this.grp1AltaBotonera.add(this.btnCancelar);
	  			
	  		this.grp1Alta.add(this.rdbTipo);
	  		this.grp1Alta.add(this.cmbRubro);
	  		this.grp1Alta.add(this.txaGlosa);
	  		this.grp1Alta.add(this.txtImporte);
	  		this.grp1Alta.add(this.grp1AltaBotonera);
	  	this.grp1.add(this.grp1Monto);
	  	this.grp1.add(this.grp1Alta);		
		
	  	
  	this.grp3 = new qx.ui.groupbox.GroupBox();
  		this.grp3.setWidth(690);
  		this.grp3.setLayout(new qx.ui.layout.Basic());
  		this.txtBuscar = new qx.ui.form.TextField();
  		this.txtBuscar.setWidth(130);
  		this.cmbBuscarRubro = new qx.ui.form.ow.ComboFilter("Rubro:",this.resRubros);
  		this.cmbBuscarRubro.getLabel().setWidth(50);
  		this.btnBuscar = new qx.ui.form.Button("Buscar");
  		this.btnReset = new qx.ui.form.Button("Resetear");
  	this.grp3.add(this.txtBuscar,{left:0,top:0});
  	this.grp3.add(this.cmbBuscarRubro,{left:150,top:0});
  	this.grp3.add(this.btnBuscar,{left:420,top:0});
  	this.grp3.add(this.btnReset,{left:500,top:0});
  		
  	this.grp2 = new qx.ui.groupbox.GroupBox();
  		this.grp2.setWidth(690);
  		this.grp2.setLayout(new qx.ui.layout.VBox(10));
    	this.mdlCajaMensual = new qx.ui.table.model.Simple();
			this.mdlCajaMensual.setColumns(["Op.N°","Fecha","Rubro","Descripción", "Ingreso", "Egreso", "Usuario"]);
				this.tblCajaMensual = new qx.ui.table.Table(this.mdlCajaMensual);
					this.tblCajaMensual.setColumnWidth(0, 50);
					this.tblCajaMensual.setColumnWidth(1, 80);
					this.tblCajaMensual.setColumnWidth(2, 130);
					this.tblCajaMensual.setColumnWidth(3, 240);
					this.tblCajaMensual.setColumnWidth(4, 70);
					this.tblCajaMensual.setColumnWidth(5, 70);
					this.tblCajaMensual.setColumnWidth(6, 80);
						this.tblCajaMensual.getTableColumnModel().setColumnVisible(6,false);
					this.tblCajaMensual.setShowCellFocusIndicator(false);
					this.tblCajaMensual.setWidth(530);
					this.tblCajaMensual.setHeight(200);					
		this.txaMostrarFull = new qx.ui.form.ow.TextArea("Detalle:");
			this.txaMostrarFull.getText().setHeight(150);
			this.txaMostrarFull.getText().setReadOnly(true);
					
	this.grp2.add(this.tblCajaMensual);
	this.grp2.add(this.txaMostrarFull);

	this.add(this.grp1,{left:10,top:0});
	this.add(this.grp3,{left:380,top:400});	
	this.add(this.grp2,{left:380,top:0});
	
	this._attachEvents();	
  },
  
/*************************************************************************************************************************

 *************************************************************************************************************************
 *MEMBERS MEMBERS MEMBERS MEMBERSMEMBERS MEMBERS MEMBERS MEMBERS MEMBERS MEMBERS MEMBERS MEMBERS MEMBERS MEMBERS MEMBERS**
 *************************************************************************************************************************
 *************************************************************************************************************************/
members : 
	{
  _attachEvents : function (){
			this.addListener('appear', this._initForm, this);
			this.btnAceptar.addListener('execute', this._btnAceptarExecute, this);
			this.tblCajaMensual.addListener('cellClick', this._tblCajaMensualClick, this);
			this.tblCajaMensual.addListener("keyup", function (e)
			{
				if ((e.getKeyIdentifier() == 'Up') || (e.getKeyIdentifier() == 'Down'))
				{
					this._tblCajaMensualClick();
				}
			}, this);
		},
		_initForm : function(){
			this.rdbTipo.setValue("E");
			this.txaGlosa.setValue("");
			this.txtImporte.setValue("");
			this._mesMostar = "";
			this._tblCajaMensualLoad();
			this.cmbRubro.setNewValues(this.resRubros,true);
			this.grp3.setVisibility("excluded");
		},	
		_btnAceptarExecute:function(){
			var params = new Object();
			params.cod_clinica = this.cod_clinica;
			var montoDisponible = this.rpcCaja.callSync("montoDisponible", params);
			if(this.cmbRubro.getValue()== null){
				alert("El rubro no existe");
			}
			else{
				if(this.txtImporte.getValue() <= montoDisponible || this.rdbTipo.getValue() == "I"){
					var params = new Object();
						params.tipo = this.rdbTipo.getValue();
						params.glosa =  this.txaGlosa.getValue();
						params.cod_caja_rubro = this.cmbRubro.getValue();
						params.importe = this.txtImporte.getValue();
						params.cod_clinica = this.cod_clinica;
						
					try{
						this.rpcCaja.callSync("insertOperacion", params);
						this.txtImporte.setValue("");
						this.txaGlosa.setValue("");
						this._initForm();
					}
					catch(ex){
						alert("Error: " + ex);
					}						
				}
				else{
					alert("El importe es superior al Monto Disponible ($"+montoDisponible+")");
				}
			}
		},
		_tblCajaMensualLoad : function(){
			try{
				var params = new Object();
				params.mesMostrar = "actual";
				params.cod_clinica = this.cod_clinica;
				var result = this.rpcCaja.callSync("getOperaciones", params);
					this.mdlCajaMensual.setData(result.values);
					this.lblIngresosV.setValue("<b><big><font color='green'>$"+result.ingresoTotal+"</font></big></b>");
					this.lblEgresosV.setValue("<b><big><font color='red'>$"+result.egresoTotal+"</font></big></b>");
					this.lblMontoDisponibleV.setValue("<b><big><font color='green'>$"+result.montoDisponible+"</font></big></b>");
			}
			catch(ex){
				alert("Error: " + ex);
			}	
		},
		_tblCajaMensualClick:function(){
			var cod_operacion = this.mdlCajaMensual.getValue(0, this.tblCajaMensual.getFocusedRow());
			var fecha = this.mdlCajaMensual.getValue(1, this.tblCajaMensual.getFocusedRow());
			var usuario = this.mdlCajaMensual.getValue(6, this.tblCajaMensual.getFocusedRow());
			var valorIngreso = this.mdlCajaMensual.getValue(4, this.tblCajaMensual.getFocusedRow());
			var valorEgreso = this.mdlCajaMensual.getValue(5, this.tblCajaMensual.getFocusedRow());
			var rubro = this.mdlCajaMensual.getValue(2, this.tblCajaMensual.getFocusedRow());
			if(valorIngreso !=""){
				var tipo = "Ingreso";
				var importe = valorIngreso;
			}
			else if(valorEgreso !=""){
				var tipo = "Egreso";
				var importe = valorEgreso;				
			}
			var glosa = this.mdlCajaMensual.getValue(3, this.tblCajaMensual.getFocusedRow());
			var mostrar = "Operación N°: "+cod_operacion +"                Fecha: "+fecha+"                Rubro: "+rubro+"                Usuario: "+usuario;
				mostrar+="\n----------------------------------------------------------------------------------------------------------------------------------------------------------\n";
				mostrar+=tipo+": "+importe+"\n";
				mostrar+="Descripción: "+glosa;
			this.txaMostrarFull.setValue(mostrar);
		}
	}
});

    